package TankGame1;

import java.io.Serializable;
import java.util.Vector;

/**
 * 坦克的父类
 */
public class TankModel implements Serializable {
    private int x;//x坐标
    private int y;//y坐标
    private int direction; //坦克方向
    private boolean isLive = true;
    private Vector<Shot> shots = new Vector<>();
    private Vector<EnemyTank> enemyTanks = null;

    public TankModel(int x, int y, int direction, Vector<EnemyTank> enemyTanks) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.enemyTanks = enemyTanks;
    }

    public void setX(int x) {
        if (x >= 0 && x <= 940) {
            if (xCollide(x))
                this.x = x;
        }
    }

    public void setY(int y) {
        if (y >= 0 && y <= 690) {
            if (yCollide(y))
                this.y = y;
        }
    }

    //往x方向移动是否合法
    public boolean xCollide(int x) {
        //当前坦克往左边走
        for (int i = 0; i < getEnemyTanks().size(); i++) {
            EnemyTank tank = getEnemyTanks().get(i);
            int x1;
            int y1;
            int direction;
            if (tank != this) {
                x1 = tank.getX();
                y1 = tank.getY();
                direction = tank.getDirection();
            } else {
                //敌人坦克和我的位置是否冲突
                MyTank myTank = tank.getMyTank();
                if (myTank == null || !myTank.isLive()) {
                    continue;
                }
                x1 = myTank.getX();
                y1 = myTank.getY();
                direction = myTank.getDirection();
            }
            switch (direction) {
                case 0:
                case 2:
                    //对方坦克上/下
                    if (getY() + 40 <= y1 || getY() >= y1 + 60) {
                        continue;
                    } else {
                        if (x1 + 40 <= x || x + 60 <= x1) {
                            continue;
                        }
                        return false;
                    }
                case 1:
                case 3:
                    //对方坦克左/右
                    if (getY() + 40 <= y1 || getY() >= y1 + 40) {
                        continue;
                    } else {
                        if (x1 + 60 <= x || x + 60 <= x1) {
                            continue;
                        }
                        return false;
                    }
            }
        }
        return true;
    }

    //往y方向移动是否合法
    public boolean yCollide(int y) {
        //当前坦克往上或下
        int x1;
        int y1;
        int direction;
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank tank = enemyTanks.get(i);
            if (tank != this) {
                x1 = tank.getX();
                y1 = tank.getY();
                direction = tank.getDirection();
            } else {
                //敌人坦克和我的位置是否冲突
                MyTank myTank = tank.getMyTank();
                if (myTank == null || !myTank.isLive()) {
                    continue;
                }
                x1 = myTank.getX();
                y1 = myTank.getY();
                direction = myTank.getDirection();
            }
            //自己和自己不用比较
            switch (direction) {
                case 0:
                case 2:
                    //对方坦克上/下
                    if (getX() + 40 <= x1 || getX() >= x1 + 40) {
                        continue;
                    } else {
                        if (y1 + 60 <= y || y + 60 <= y1) {
                            continue;
                        }
                        return false;
                    }
                case 1:
                case 3:
                    //对方坦克左/右
                    if (getX() + 40 <= x1 || getX() >= x1 + 60) {
                        continue;
                    } else {
                        if (y1 + 40 <= y || y + 60 <= y1) {
                            continue;
                        }
                        return false;
                    }
            }
        }
        return true;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getDirection() {
        return direction;
    }

    public Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public Vector<Shot> getShots() {
        return shots;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public boolean isLive() {
        return isLive;
    }
}
